package org.jhulk.states;

import java.util.Iterator;

import org.jhulk.sprites.Marine;
import org.jhulk.sprites.Squad;
import org.jhulk.utils.TileMapUtils;
import org.newdawn.slick.AngelCodeFont;
import org.newdawn.slick.Color;
import org.newdawn.slick.Font;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.state.BasicGameState;
import org.newdawn.slick.state.StateBasedGame;

public class PlacementState extends BasicGameState {

	/** The ID given to this state */
	public static final int ID = 1;
	
	private GameCache cache;
	
    /** The font to write the message with */ 
    private Font font; 
	
    /** The game holding this state */ 
    private StateBasedGame game; 
    
    public PlacementState(GameCache cache)
    {
    	this.cache = cache;
    }
	
	@Override
	public int getID() {
		return ID;
	}

	public void init(GameContainer container, StateBasedGame game) 
		throws SlickException 
	{
		try {
			this.game = game; 
			font = new AngelCodeFont("data/fonts/demo2.fnt","data/fonts/demo2_00.tga"); 
		} catch (SlickException e) {
			e.printStackTrace();
		}
	}


	public void render(GameContainer c, StateBasedGame sbg, Graphics g) throws SlickException {
		
		cache.getMap().render(0, 0);

		//If the cursor is over a valid tile, highlight the tile
		if(cache.getHovTileId() != 0)
			g.drawRect(
					cache.getMapXForTile().floatValue(), 
					cache.getMapYForTile().floatValue(), 
					cache.getMap().getTileWidth(), 
					cache.getMap().getTileHeight());
		
		//Draw a rectangle around the selected character
		if(cache.getSelectedChar() != null)
		{
			g.setColor(Color.cyan);
			Marine selChar = cache.getSelectedChar();
			g.drawRect(
					selChar.getX(), 
					selChar.getY(), 
					cache.getMap().getTileWidth(), 
					cache.getMap().getTileHeight());
		}
		
		//Loop through all characters
		
		////Loop through all squads
		for(Squad s : cache.getSquads()) 
		{
			s.renderSquad();
		}
	}

	public void update(GameContainer arg0, StateBasedGame arg1, int arg2)
			throws SlickException {}
	
	 public void mouseReleased(int button, int x, int y)
    {
    	cache.setCurrTile( TileMapUtils.getMapTileFromClick(x, y, cache.getMap()) );
    	placeMarine();
    }
	 
    public void placeMarine()
    {
        if(cache.getCurrTile().getId() != -1)
        {
        	for (Iterator<Marine> it = cache.getPlacementQueue().iterator(); it.hasNext (); )
        	{
        		Marine m = it.next();
        		if(m.isPlaced() == false)
    			{
    				m.placeChar(cache.getCurrTile().getMapX(), cache.getCurrTile().getMapY());
    				System.out.println("Placed : " + m.getName());
    				it.remove();
    				break;
    			}
        	}
        	
        	if(cache.getPlacementQueue().size() == 0)
        	{
        		System.out.println("Unit placement phase over, entering PLAY state");
        		game.enterState(PlayState.ID);
        	}
        }
    }
   
   public void mouseMoved(int oldx, int oldy, int newx, int newy)
   {
   	 cache.setHovTileX( TileMapUtils.getTileXfromMapX(newx, cache.getMap()) );
	     cache.setHovTileY( TileMapUtils.getTileYfromMapY(newy, cache.getMap()) );
	     
	     cache.setHovTileId( cache.getMap().getTileId(cache.getHovTileX(), cache.getHovTileY(), 0) );
	     
	     cache.setMapXForTile( new Double(TileMapUtils.getTileXBorderFromMapX(newx, cache.getMap())) );
	     cache.setMapYForTile( new Double(TileMapUtils.getTileYBorderFromMapY(newy, cache.getMap())) );
   }

}
